﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using efuture.http;
using efuture.xml;
using System.IO;

namespace Main
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            String xmlData = "";

            HttpHelper.ServerURL = edtServerURL.Text;

            xmlData = "<UserBean><User_code>{0}</User_code><User_password>{1}</User_password></UserBean>";
            xmlData = XMLHelper.SetXml(String.Format(xmlData, edtUserCode.Text, edtPassword.Text) );
            xmlData = HttpHelper.DoPost("/rest/sys/user/login", xmlData, null);
            //MessageBox.Show(xmlData);

            if (XMLHelper.ShowException(xmlData, true)) 
            {
                return;
            }

            HttpHelper.UserId = XMLHelper.SelectSingleValue(xmlData, "/Root/UserBean/User_id");
            HttpHelper.UserCode = XMLHelper.SelectSingleValue(xmlData, "/Root/UserBean/User_code");

            HttpHelper.DoGet(String.Format("/rest/sys/user/auth?user_id={0}", HttpHelper.UserId), null);

            this.DialogResult = DialogResult.OK;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2;
            this.Top = (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}